<%@include file="/WEB-INF/templates/configuration.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@page import="nl.hva.web.mvcwebapp.models.UserTest"%>
<%
HttpSession session_user = request.getSession(true);
    String firstname = (String) session_user.getAttribute("user_firstname");
    int user_id = 0;
    int user_type=0;
    if(firstname!=null){
        user_id = Integer.valueOf("" + session_user.getAttribute("user_id"));
        user_type = Integer.valueOf("" + session_user.getAttribute("user_type"));
    }

%>
<h2>Tests</h2>
<c:choose>
    <c:when test="${amounttests != 0}">
        <!-- Wanneer er gebruikers opgeslagen zijn, worden ze hier getoond -->
        <table class="zebra">
            <tr>
                <td style="width:100px"> 
                    <strong>Test Name</strong>
                </td>
                <td style="width:200px">
                    <strong>Test Description</strong>
                </td>
                <td style="width:100px">
                    <strong>Completed?</strong>
                </td>
                <td style="width:100px">
                    <strong>Score</strong>
                </td>
                <td></td>
            </tr>
            <c:forEach var="tempTest" items="${tests}">
                <!-- Per gebruiker wordt nu een rij aangemaakt met daarin zijn gegevens -->
                <%
                    int completed = 0;
                    int score = 0;
                    int done = 0;
                %>
                <c:choose>
                    <c:when test="${madetests.size() != 0}">
                        <c:forEach var="tempMadeTest" items="${madetests}">
                            <c:choose>
                                <c:when test="${tempTest.testid == tempMadeTest.pk.test.testid}">
                                    <% 
                                    UserTest usertest = (UserTest) pageContext.getAttribute("tempMadeTest");
                                    completed = usertest.getCompleted(); 
                                    score = usertest.getScore();
                                    done = 1;
                                    %>


                                </c:when>
                                <c:otherwise>
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                    </c:when>
                </c:choose>
                <tr class="<%=(done==0)?"":(completed==1)?"test-completed":"test-failed"%>">
                    <td>
                        <%
                            if(done==0){
                        %>
                        <a href="javascript:if(confirm('Are you sure that you want to do this test?'))
                           window.location='${url}chapter/assignments?testid=${tempTest.testid}';">${tempTest.testname}</a>
                        <%
                    } else{
                        %>
                        ${tempTest.testname}
                        <%      
                    }
                        %>
                    </td>
                    <td style="width:200px">
                        <div class="description">${tempTest.testdescription}</div>
                    </td>
                    <td  style="width:100px">
                        <div style="height:30px; width:30px" class="<%=(done==0)?"":(completed==1)?"check-icon":"cross-icon"%>"><%=(done==0)?"-":""%></div>
                    </td>
                    <td style="width:100px">
                        <%=(done==0)?"-":score+"%"%>
                    </td>
                    <td style="width:100px">
                        <% if (done==0){
                            %>
                                <a class="icon view" href="javascript:if(confirm('Are you sure that you want to do this test?'))
                           window.location='${url}chapter/assignments?testid=${tempTest.testid}';">edit</a> 
                            <% 
                        }%>
                                                    <%if (user_type == 2 || user_type == 3) {
%>
                        <a class="icon edit" href="${url}chapter/test/edit?testid=${tempTest.testid}">edit</a> 
                        <a class="icon delete" href="javascript:if(confirm('Are you sure you want to delete this test?'))
                           window.location='${url}chapter/test/delete?testid=${tempTest.testid}&chapterid=${chapter.chapterid}';">delete</a>
                           
                           <%}%>
                    </td>
                </tr>
            </c:forEach>
        </table>
    </c:when>
    <c:otherwise>
        <!-- Als er geen cursussen zijn, wordt deze melding getoond -->
        There are no tests found.
    </c:otherwise>
</c:choose>

<p>
    <a class="button" href="${url}index">Back to index</a>
    <a class="button" href="${url}chapter/test/new?chapterid=${chapter.chapterid}">Add Test</a>
     <a class="button" href="${url}chapter/view?chapterid=${chapter.chapterid}">Back to Chapter</a>
</p>